﻿import kliment.math.Calc;
/**
 * ...
 * @author	Kliment
 * @version	1.1.5
 */
class kliment.utils._String {
	
	static public function addZero(value:Number, countZero:Number):String {
		var count:Number = countZero || 2;
		var valueAbs:Number = Math.abs(value);
		var leftLenght:Number = String(valueAbs >> 0).length;
		var preffix:String = repeat('0', count - leftLenght);
		if (value < 0)
			preffix = '-' + preffix;
		return preffix + valueAbs;
	}
	
	static public function insert(source:String, position:Number, repl:String):String {
		return source.substring(0, position) + repl + source.substring(position, source.length-1);
	}
	
	static public function repeat(str:String, count:Number):String {
		var result:String = '';
		while (count-- > 0)	result += str;
		return result;
	}
	
	static public function decimalFractions(value:Number, fractionsPart:Number):String {
		var right:Number = Calc.decimalPart(value, fractionsPart);
		var rightStr:String;
		var rightLength:Number;
		if (right > 0) {
			rightStr = String(right);
			rightStr = rightStr.substring(1, rightStr.length);
			rightLength = rightStr.length - 1;
		} else {
			rightStr = '.';
			rightLength = 0;
		}
		return String(Math.floor(value) + rightStr + repeat('0', fractionsPart - rightLength));
	}
	
	static public function numberGroups(value:Number, delimer:String, decimalPart:Number):String {
		delimer = delimer || ' ';
		var valueAbs:Number = Math.abs(value);
		var decimalPartStr:String = decimalFractions(Calc.decimalPart(valueAbs, decimalPart), decimalPart).substring(2, decimalPartStr.length);
		if (decimalPartStr.length > 0) 
			decimalPartStr = '.' + decimalPartStr;
		var numString:String = String(Math.floor(valueAbs));
		var length:Number = numString.length;
		var arrayString:Array = new Array();
		var start:Number;
		while (length > 0) {
			start = Math.max(0, length - 3);
			arrayString.unshift(numString.substring(start, length));
			length = start;
		}
		return (valueAbs != value?'-':'') + arrayString.join(delimer) + decimalPartStr;
	}
	
	/**
	 * Make a string's first character uppercase
	 * 
	 * @param	str The input string.
	 * @return	Returns the resulting string.
	 */
	static public function ucfirst(str:String):String {
		return getFirstChar(str).toUpperCase() + str.substring(1, str.length);
	}
	
	/**
	 * Make a string's first character uppercase, rest lowercase
	 * 
	 * @param	str The input string.
	 * @return	Returns the resulting string.
	 */
	static public function ucfirst_lcrest(str:String):String {
		return ucfirst(str.toLowerCase());
	}
	
	/**
	 * Make a string's first character lowercase
	 * 
	 * @param	str The input string.
	 * @return	Returns the resulting string.
	 */
	static public function lcfirst(str:String):String {
		return getFirstChar(str).toLowerCase() + str.substring(1, str.length);
	}
	
	/**
	 * Make a string's first character lowercase, rest uppercase
	 * 
	 * @param	str The input string.
	 * @return	Returns the resulting string.
	 */
	static public function lcfirst_ucrest(str:String):String {
		return lcfirst(str.toUpperCase());
	}
	
	/**
	 * Get first char from string;
	 * 
	 * @param	str The input string.
	 * @return	Returns the resulting string
	 */
	static public function getFirstChar(str:String):String {
		return str.substring(0, 1);
	}
}